New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
polish: skip creating extra reference for safely re-used node #10720
Conversation
scope.push({ id: ref }); | ||
let ref, assignment; | ||
// skip creating extra reference when `left` is semantically safe to re-use | ||
if (t.isIdentifier(node.left)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about using isConstantExpression
, or maybe better maybeGenerateMemoised
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! I end up with maybeGenerateMemoised
because it is rare that the left
is a literal --even if it is, creating a reference other than inlining this literal should costs less bytes in most situations, given that we generate [_]+
as identifier name for literals.
ref = node.left; | ||
assignment = t.cloneNode(node.left); | ||
} else { | ||
assignment = t.assignmentExpression("=", ref, node.left); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ref
is non-null, it is always cloned from node.left
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to figure out why CircleCI isn't running for your PRs
In this PR we skip creating extra reference when the
left
of??
is a static reference, which is safely to reused when later unfolding into conditional comparison.Compared to the master branch, this PR generate less code for the common case
var bar = foo ?? "bar"
Before:
After: